//
//  GameCell.m
//  YoucApp
//
//  Created by Liam on 11/13/12.
//
//

#import "GameCell.h"
#import <QuartzCore/QuartzCore.h>
#import "UIView+ModalAnimation.h"
#import "AttentionManager.h"
#import <CustomBadge.h>

@interface GameCell ()

@property (retain, nonatomic) NSString *gameId;
@property (retain, nonatomic) CustomBadge *customBadge;

@end

@implementation GameCell

@synthesize gameImage = _gameImage;
@synthesize gameName = _gameName;
@synthesize gameType = _gameType;
@synthesize toggle = _toggle;
@synthesize gameId = _gameId;
@synthesize attentionButton = _attentionButton;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)prepareForReuse
{
    [super prepareForReuse];
    
	self.gameImage.image = nil;
    [self setExpand:NO animated:NO];
	[self setTodayNum:0];
}

- (void)dealloc {
    [_gameName release];
    [_gameImage release];
    [_gameType release];
    [_toggle release];
	[_gameId release];
	[_attentionButton release];
	[_customBadge release];
    [super dealloc];
}

- (IBAction)AddFav:(id)sender {
    [[AttentionManager instance] addAttention:self.gameId];
}

- (void)setName:(NSString *)name
{
    self.gameName.text = name;
}

- (void)setType:(NSString *)type
{
    self.gameType.text = type;
}

- (void)setImageUrl:(NSString *)urlString
{
    [self.gameImage loadImageFromURL:[NSURL URLWithString:urlString] showLoading:NO];
}

- (void)setTodayNum:(NSInteger)num
{
	if (num > 0) {
		NSString * strNum = [NSString stringWithFormat:@"%d", num];
		if (self.customBadge) {
			[self.customBadge autoBadgeSizeWithString:strNum];
		}
		else {
			self.customBadge = [CustomBadge customBadgeWithString:strNum];
			[self addSubview:self.customBadge];
			CGRect frame = self.customBadge.frame;
			frame.origin.x = CGRectGetMaxX(self.gameImage.frame) - 20;
			frame.origin.y = frame.origin.y - 2;
			self.customBadge.frame = frame;
		}
		self.customBadge.hidden = NO;
		
	}
	else {
		self.customBadge.hidden = YES;
	}
}

#define RADIANS(degrees) ((degrees * (float)M_PI) / 180.0f)

- (void)setExpand:(BOOL)bExpand animated:(BOOL)animated
{
    CGAffineTransform transform = CGAffineTransformIdentity;
    if (bExpand)
    {
        transform = CGAffineTransformMakeRotation(RADIANS(90));
    }
    if (animated)
    {
        [UIView beginAnimations:nil context:UIGraphicsGetCurrentContext()];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationDuration:0.2f];
    }
    self.toggle.transform = transform;
    if (animated)
        [UIView commitModalAnimations];
}

- (void)setGameId:(NSString *)gameId
{
	SAFE_RELEASE(_gameId);
	_gameId = [gameId retain];
}

- (void)setIsAttention:(BOOL)isAttention
{
	self.attentionButton.selected = isAttention;
}

@end
